package com.shiwaixiangcun.customer.module.intelligent.camerautil;

import android.os.Handler;
import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.shiwaixiangcun.customer.event.EventUtil;
import com.shiwaixiangcun.customer.event.SimpleEvent;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class OpenApiHelper {

    public final static String tag = "OpenApiHelper";


    public static void getAccessToken(final String host,
                                      final String phoneNumber, final String appid,
                                      final String appsecret, final Handler handler) {
        // 拼装url
        String url = "";
        if (host.endsWith(":443")) {
            url = "https://" + host + "/openapi/accessToken";
        } else {
            url = "http://" + host + "/openapi/accessToken";
        }
        Log.d(tag, url);
        getToken(url, phoneNumber, appid, appsecret, handler);
    }

    /**
     * 获取用户Token
     *
     * @param host
     * @param phoneNumber
     * @param appid
     * @param appsecret
     * @param handler
     */
    public static void getUserToken(final String host,
                                    final String phoneNumber, final String appid,
                                    final String appsecret, final Handler handler) {
        // 拼装url
        String url = "";
        if (host.endsWith(":443")) {
            url = "https://" + host + "/openapi/userToken";
        } else {
            url = "http://" + host + "/openapi/userToken";
        }
        Log.d(tag, url);
        getToken(url, phoneNumber, appid, appsecret, handler);
    }

    public static void userBindSms(final String host, final String phoneNumber,
                                   final String appid, final String appsecret, final Handler handler) {
        String url = "";
        if (host.endsWith(":443")) {
            url = "https://" + host + "/openapi/userBindSms";
        } else {
            url = "http://" + host + "/openapi/userBindSms";
        }
        Log.d(tag, url);
        // 生成HttpRequest对象
        HttpRequestBase httpRequest;
        HttpPost httpPost = new HttpPost(url);
        try {
            JSONObject body = new JSONObject();
            String data = "{phone: \"" + phoneNumber + "\"}";
            body.put("params", new JSONObject(data));
            body.put("id", "1");// id号 随机值

            body.put(
                    "system",
                    new JSONObject(SignHelper.getSystem(data, appid, appsecret,
                            "1.1")));
            httpPost.setEntity(new StringEntity(body.toString(), "utf-8"));
            Log.d(tag, body.toString());
            httpPost.setHeader("Content-Type", "application/json");
        } catch (Exception e) {

            e.printStackTrace();
        }
        httpRequest = httpPost;

        // 设置超时时间
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        HttpConnectionParams.setSoTimeout(params, 5000);
        httpRequest.setParams(params);
        SingleClientConnManager manager = null;
        int code = -1;
        String result = "";
        try {
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), 80));
            registry.register(new Scheme("https", SSLSocketFactory
                    .getSocketFactory(), 443));
            manager = new SingleClientConnManager(httpRequest.getParams(),
                    registry);
            HttpClient httpclient = new DefaultHttpClient(manager,
                    httpRequest.getParams());

            HttpResponse httpResponse = httpclient.execute(httpRequest);
            StatusLine status = httpResponse.getStatusLine();
            code = status.getStatusCode();
            if (code == HttpStatus.SC_OK) {
                // 状态为正常时，进行body内容获取
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                    Log.d(tag, result);
                    JSONObject res = new JSONObject(result);

                    // 简单做法 直接传出
                    if (res.getJSONObject("result").getString("code")
                            .equals("0")) {
                        code = 0;
                        result = res.getJSONObject("result").getString("msg");
                    } else {
                        code = -1;
                        result = res.getJSONObject("result").getString("msg");
                    }
                }
            } else {
                result = status.getReasonPhrase();
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            manager.shutdown();
        }
        handler.obtainMessage(code, result).sendToTarget();
    }

    /**
     * 免短信验证绑定用户
     *
     * @param host
     * @param phoneNumber
     * @param appId
     * @param appSecret
     * @param handler
     */
    public static void userBindNoSMS(final String host, final String phoneNumber,
                                     final String appId, String appSecret, final Handler handler) {

        String url = "";
        if (host.endsWith(":443")) {
            url = "https://" + host + "/openapi/userBindNoSms";
        } else {
            url = "http://" + host + "/openapi/userBindNoSms";
        }
        JSONObject body = new JSONObject();
        try {

            String data = "{phone: \"" + phoneNumber + "\"}";
            body.put("params", new JSONObject(data));
            // id号 随机值
            body.put("id", "1");
            body.put(
                    "system",
                    new JSONObject(SignHelper.getSystem(data, appId, appSecret,
                            "1.1")));
        } catch (Exception e) {

            e.printStackTrace();
        }

        OkGo.<String>post(url)
                .upJson(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {

                        EventUtil.getInstance().postSticky(new SimpleEvent(SimpleEvent.GET_LE_TOKEN, 1, response.body()));

                    }
                });

    }


    /**
     * 获取用户Token
     *
     * @param host
     * @param phoneNumber
     * @param appId
     * @param appSecret
     * @param smsCode
     * @param handler
     */
    public static void userBind(final String host, final String phoneNumber,
                                final String appId, String appSecret, final String smsCode,
                                final Handler handler) {
        String url = "";
        if (host.endsWith(":443")) {
            url = "https://" + host + "/openapi/userBind";
        } else {
            url = "http://" + host + "/openapi/userBind";
        }
        JSONObject body = new JSONObject();
        try {

            String data = "{phone: \"" + phoneNumber + "\",smsCode:\"" + smsCode
                    + "\"}";
            body.put("params", new JSONObject(data));
            // id号 随机值
            body.put("id", "1");
            body.put("system", new JSONObject(SignHelper.getSystem(data, appId, appSecret,
                    "1.1")));

        } catch (Exception e) {

            e.printStackTrace();
        }

        OkGo.<String>post(host)
                .upJson(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {

                        EventUtil.getInstance().postSticky(new SimpleEvent(SimpleEvent.GET_LE_TOKEN, 1, response.body()));

                    }
                });

//        handler.obtainMessage(code, result).sendToTarget();
    }

    private static void getToken(final String host, final String phoneNumber,
                                 final String appid, final String appsecret, final Handler handler) {
        //创建okHttpClient对象
        JSONObject jsonBody = new JSONObject();
        try {
            String data = "{phone:\"" + phoneNumber + "\"}";
            jsonBody.put("params", new JSONObject(data));
            // id号 随机值
            jsonBody.put("id", "1");
            jsonBody.put("system", new JSONObject(SignHelper.getSystem(data, appid, appsecret,
                    "1.1")));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkGo.<String>post(host)
                .upJson(jsonBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {

                        EventUtil.getInstance().postSticky(new SimpleEvent(SimpleEvent.GET_LE_TOKEN, 1, response.body()));

                    }
                });
    }
}
